//
//  LZNotSubmitCouponCell.m
//  laziz_Merchant
//
//  Created by xbnzd on 16/12/20.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZNotSubmitCouponCell.h"
#import "LZCouponListModel.h"
#import "UIImageView+WebCache.h"
#import "LZTimeLimitCountDownJudgeDataManager.h"
#import "XBNTimeLimitCountDownCalculator.h"
#import "UIView+Rotation.h"

@interface LZNotSubmitCouponCell()
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *rightArrowImg;
@property (weak, nonatomic) IBOutlet UILabel *topRightLabel;
@property (weak, nonatomic) IBOutlet UIImageView *vidoeCoverImgView;
@property (weak, nonatomic) IBOutlet UILabel *couponLabel;
@property (weak, nonatomic) IBOutlet UILabel *couponCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *groupLabel;
@property (weak, nonatomic) IBOutlet UILabel *groupCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *remainTimeLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *deleteBtnWidth;
@property (weak, nonatomic) IBOutlet UILabel *timeCountDownLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *groupLabelTopToCouponLabelConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *groupLabelTopToPlayBgViewConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *deleteBtnTopToCouponLabelConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *deleteBtnTopToTimeCountDownLabelConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *deleteBtnTopToPlayBgViewConstraint;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@property (weak, nonatomic) IBOutlet UIButton *editBtn;
@property (weak, nonatomic) IBOutlet UIButton *deleteBtn;
@property (strong, nonatomic) XBNTimeLimitCountDownCalculator *timeCountDownCalculator;

//@property (nonatomic, strong) LZCouponListModel *dataModel;
@property (nonatomic, strong) NSIndexPath *indexP;

@end

@implementation LZNotSubmitCouponCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _deleteBtnWidth.constant = (SCREEN_WIDTH - 60)/3;
    [self configureBtnTitle];
    [self configureBtnLayer:self.deleteBtn];
    [self configureBtnLayer:self.editBtn];
    [self configureBtnLayer:self.submitBtn];
    
    self.vidoeCoverImgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playAction)];
    [self.vidoeCoverImgView addGestureRecognizer:tap];
    
     [self.rightArrowImg setLanguageRotaion];
}

#pragma mark -
#pragma mark public
- (void)loadContentWithModel:(LZCouponListModel *)dataModel indePath:(NSIndexPath *)indexPath
{
    self.dataModel = dataModel;
    self.indexP = indexPath;
    
    self.nameLabel.text = dataModel.promotionTitle;
    [self.vidoeCoverImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",APIBaseImageUrl,dataModel.videoPhoto]] placeholderImage:[UIImage imageNamed:@"videoCoverImg"]];
    
    //优惠券,团购 倒计时 内容
    [self setCouponAndGroupContentWith:dataModel];
    
    //设置 优惠券,,团购, 按钮 frame
    [self setCouponAndGroupAndBtnFrameWith:dataModel];
    
    //设置 底部 按钮显示状态
    [self setFootButtonShowingWith:dataModel];
    
}

- (void)startTimeDown
{
//团购===========
    self.timeCountDownCalculator.remainTimeInterval = self.dataModel.grouponEndTime/1000 - self.dataModel.nowTime;
    
    if (self.timeCountDownCalculator.remainTimeInterval<=0) {
        [LZTimeLimitCountDownJudgeDataManager configLimitedLabel:self.timeCountDownLabel withReamin:0 andHourUnit:@":" andMinuteUnit:@":" andSecUnit:@"" andIsChangeUintColor:YES andHourUnitColor:UIColorFromRGB(0x6f6f6f, 1) andMinUnitColor:UIColorFromRGB(0x666666, 1) andSecUnitColor:UIColorFromRGB(0x666666, 1)];
        [self.timeCountDownCalculator timeLimitCountDownClear];
    }
    else {
        WS(weakSelf);
        
        [self.timeCountDownCalculator addProcessBlock:^(NSTimeInterval remain) {
            __strong typeof(weakSelf)strongSelf = weakSelf;
            if (remain<0) {
                [LZTimeLimitCountDownJudgeDataManager configLimitedLabel:strongSelf.timeCountDownLabel withReamin:0 andHourUnit:@":" andMinuteUnit:@":" andSecUnit:@"" andIsChangeUintColor:YES andHourUnitColor:UIColorFromRGB(0x666666, 1) andMinUnitColor:UIColorFromRGB(0x666666, 1) andSecUnitColor:UIColorFromRGB(0x666666, 1)];
                [weakSelf.timeCountDownCalculator timeLimitCountDownClear];
            }
            else {
                [LZTimeLimitCountDownJudgeDataManager configLimitedLabel:strongSelf.timeCountDownLabel withReamin:remain andHourUnit:@":" andMinuteUnit:@":" andSecUnit:@"" andIsChangeUintColor:YES andHourUnitColor:UIColorFromRGB(0x666666, 1) andMinUnitColor:UIColorFromRGB(0x666666, 1) andSecUnitColor:UIColorFromRGB(0x666666, 1)];
            }
            
        } cancelBlock:^{
            //        DLog(@"结束了");
        }];
        
        [self.timeCountDownCalculator timeLimitCountDownResume];
    }
        

}

#pragma mark -
#pragma mark btn action
- (void)playAction
{
    if (self.cellEditOrDetailDelegate && [self.cellEditOrDetailDelegate respondsToSelector:@selector(clickEditOrDetailOrPlayVideoBtnWithIndexPath:couponBtnCode: withCell:)]) {
        [self.cellEditOrDetailDelegate clickEditOrDetailOrPlayVideoBtnWithIndexPath:self.indexP couponBtnCode:kCouponList_playVideo withCell:self];
    }
}

- (IBAction)topRightButton:(UIButton *)sender {
    if (self.cellEditOrDetailDelegate && [self.cellEditOrDetailDelegate respondsToSelector:@selector(clickEditOrDetailOrPlayVideoBtnWithIndexPath:couponBtnCode: withCell:)]) {
        [self.cellEditOrDetailDelegate clickEditOrDetailOrPlayVideoBtnWithIndexPath:self.indexP couponBtnCode:kCouponList_detail withCell:self];
    }
}
- (IBAction)deleteBtnAction:(UIButton *)sender {
    if (self.cellClickDelegate && [self.cellClickDelegate respondsToSelector:@selector(clickCellButtonWithIndex: andCell:)]) {
        [self.cellClickDelegate clickCellButtonWithIndex:kCouponList_delete andCell:self];
    }
}
- (IBAction)editBtnAction:(UIButton *)sender {
    if (self.cellEditOrDetailDelegate && [self.cellEditOrDetailDelegate respondsToSelector:@selector(clickEditOrDetailOrPlayVideoBtnWithIndexPath:couponBtnCode: withCell:)]) {
        [self.cellEditOrDetailDelegate clickEditOrDetailOrPlayVideoBtnWithIndexPath:self.indexP couponBtnCode:kCouponList_edit withCell:self];
    }
}
- (IBAction)submitBtnAction:(UIButton *)sender {
    if (self.cellClickDelegate && [self.cellClickDelegate respondsToSelector:@selector(clickCellButtonWithIndex: andCell:)]) {
        [self.cellClickDelegate clickCellButtonWithIndex:kCouponList_submit andCell:self];
    }
}

#pragma mark -
#pragma mark private

/**
 设置 优惠券名称, 团购名称,还有倒计时
 
 @param dataModel <#dataModel description#>
 */
- (void)setCouponAndGroupContentWith:(LZCouponListModel *)dataModel
{
    //优惠券===========
    if (dataModel.couponsName.length > 0) {
        self.couponLabel.text = LZLocalizedString(@"coupons", nil);
        self.couponCountLabel.attributedText = [self configureTextColorWithUseCount:[NSString stringWithFormat:@"%d",dataModel.couponsGetCount] TotalCount:[NSString stringWithFormat:@"%d",dataModel.couponsTotalCount]];
        
    }
    
    //团购===========
    if (dataModel.grouponName.length > 0) {
        self.groupLabel.text = LZLocalizedString(@"group_order", nil);
        self.groupCountLabel.text = [NSString stringWithFormat:@"%d",dataModel.grouponFinishCount];
        
        //倒计时
        self.remainTimeLabel.text = LZLocalizedString(@"remaining_time", nil);
    }
    
    
}


/**
 UI判断 根据值,判断应该显示哪些UI
 
 @param dataModel <#dataModel description#>
 */
- (void)setCouponAndGroupAndBtnFrameWith:(LZCouponListModel *)dataModel
{
    //UI判断========
    if (dataModel.couponsName.length > 0 && dataModel.grouponName.length == 0) {//只有优惠券
        self.remainTimeLabel.hidden = YES;
        self.timeCountDownLabel.hidden = YES;
        self.groupLabel.hidden = YES;
        self.groupCountLabel.hidden = YES;
        self.couponLabel.hidden = NO;
        self.couponCountLabel.hidden = NO;
        
        self.deleteBtnTopToCouponLabelConstraint.priority = 999;
        self.deleteBtnTopToTimeCountDownLabelConstraint.priority = 250;
        self.deleteBtnTopToPlayBgViewConstraint.priority = 250;
        
    }else if (dataModel.couponsName.length == 0 && dataModel.grouponName.length > 0){//只有团购
        self.remainTimeLabel.hidden = NO;
        self.timeCountDownLabel.hidden = NO;
        self.groupLabel.hidden = NO;
        self.groupCountLabel.hidden = NO;
        self.couponLabel.hidden = YES;
        self.couponCountLabel.hidden = YES;
        
        self.groupLabelTopToPlayBgViewConstraint.priority = 999;
        self.groupLabelTopToCouponLabelConstraint.priority = 250;
        self.deleteBtnTopToTimeCountDownLabelConstraint.priority = 999;
        self.deleteBtnTopToCouponLabelConstraint.priority = 250;
        self.deleteBtnTopToPlayBgViewConstraint.priority = 250;
    }else if (dataModel.couponsName.length == 0 && dataModel.grouponName.length == 0){//优惠券,团购都没有
        self.remainTimeLabel.hidden = YES;
        self.timeCountDownLabel.hidden = YES;
        self.groupLabel.hidden = YES;
        self.groupCountLabel.hidden = YES;
        self.couponLabel.hidden = YES;
        self.couponCountLabel.hidden = YES;
        
        self.deleteBtnTopToPlayBgViewConstraint.priority = 999;
        self.deleteBtnTopToTimeCountDownLabelConstraint.priority = 250;
        self.deleteBtnTopToCouponLabelConstraint.priority = 250;
    }else if (dataModel.couponsName.length > 0 && dataModel.grouponName.length > 0){//优惠券,团购都有
        self.remainTimeLabel.hidden = NO;
        self.timeCountDownLabel.hidden = NO;
        self.groupLabel.hidden = NO;
        self.groupCountLabel.hidden = NO;
        self.couponLabel.hidden = NO;
        self.couponCountLabel.hidden = NO;
        
        self.deleteBtnTopToPlayBgViewConstraint.priority = 250;
        self.deleteBtnTopToTimeCountDownLabelConstraint.priority = 999;
        self.deleteBtnTopToCouponLabelConstraint.priority = 250;
        
        self.groupLabelTopToPlayBgViewConstraint.priority = 250;
        self.groupLabelTopToCouponLabelConstraint.priority = 999;
    }
    
    
}


/**
 设置底部按钮显示状态

 @param dataModel <#dataModel description#>
 */
- (void)setFootButtonShowingWith:(LZCouponListModel *)dataModel
{
    if (dataModel.state == kCouponDetail_saved) { //待提交 == 已保存
        self.deleteBtn.hidden = NO;
        self.editBtn.hidden = NO;
        self.submitBtn.hidden = NO;
        self.topRightLabel.text = LZLocalizedString(@"saved", nil);
    }else if (dataModel.state == kCouponDetail_rejected){ //已驳回
        self.deleteBtn.hidden = NO;
        self.editBtn.hidden = NO;
        self.submitBtn.hidden = YES;
        self.topRightLabel.text = LZLocalizedString(@"rejected", nil);
    }
}


/**b
 配置按钮标题国际化
 */
- (void)configureBtnTitle
{
    [self.deleteBtn setTitle:LZLocalizedString(@"delete", nil) forState:UIControlStateNormal];
    [self.editBtn setTitle:LZLocalizedString(@"edit", nil) forState:UIControlStateNormal];
    [self.submitBtn setTitle:LZLocalizedString(@"submit", nil) forState:UIControlStateNormal];
}


/**
 设置按钮圆角 边线

 @param btn <#btn description#>
 */
- (void)configureBtnLayer:(UIButton *)btn
{
    btn.layer.borderColor = UIColorFromRGB(0xcccccc, 1).CGColor;
    btn.layer.borderWidth = 0.5;
    btn.layer.cornerRadius = 5;
    btn.layer.masksToBounds = YES;
}


/**
 返回优惠券 不同颜色 string

 @param useCount <#useCount description#>
 @param totalCount <#totalCount description#>
 @return <#return value description#>
 */
- (NSMutableAttributedString *)configureTextColorWithUseCount:(NSString *)useCount TotalCount:(NSString *)totalCount
{
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@/%@",useCount,totalCount]];
    [attStr addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0x36bb2a, 1) range:NSMakeRange(0, useCount.length)];
    
    return attStr;
}


- (XBNTimeLimitCountDownCalculator *)timeCountDownCalculator
{
    if (!_timeCountDownCalculator) {
        _timeCountDownCalculator = [XBNTimeLimitCountDownCalculator new];
    }
    
    return _timeCountDownCalculator;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
